package packageController;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import packageBusiness.CategorieManager;
import packageBusiness.RechercheManager;
import packageException.*;
import packageModel.Categorie;
import packageModel.Syllabus;

/**
 *
 * @author Lionel & Julien
 */
public class RechercheSyllatheque extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {            
            // ---------------------------
            // Variable passé en requête
            String motClef = request.getParameter("motClef");
            String categorie = request.getParameter("categorie");
            
            // ---------------------------
            // Création de la liste des syllabus recherché
            ArrayList<Syllabus> listSyllabus = new ArrayList<Syllabus>();
            RechercheManager listSyl = new RechercheManager();
            
            // Création du syllabus
            // Est-ce qu'une catégorie a été sélectionnée ?
            if(!(categorie.equals("all"))) {
                // ---------------------------
                // Récupération de la catégorie correspondant au syllabus de la recherche
                CategorieManager nomCat = new CategorieManager();            
                Categorie cat = nomCat.getCategorie(Integer.parseInt(categorie));
                
                // ---------------------------
                // Création des variables pour la vue
                listSyllabus = listSyl.SyllabusRecherche(motClef, categorie);
                request.setAttribute("categorie", cat.getDenomination());
            } else {
                // ---------------------------
                // Récupération de la liste des syllabus
                listSyllabus = listSyl.SyllabusRecherche(motClef);
            }

            // ---------------------------
            // Création des variables pour la vue
            request.setAttribute("motClef", motClef);
            request.setAttribute("nbElement", listSyllabus.size());
            request.setAttribute("listSyllabus", listSyllabus);

            // ---------------------------
            // Redirection vers une page pour la vue
            RequestDispatcher rd = request.getRequestDispatcher("recherche.jsp");
            rd.forward(request, response);
        } catch(SyllabusException e1) {
            // Erreur
            request.setAttribute("error", e1.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(LangueException e2) {
            // Erreur
            request.setAttribute("error", e2.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(CategorieException e3) {
            // Erreur
            request.setAttribute("error", e3.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(AuteurException e4) {
            // Erreur
            request.setAttribute("error", e4.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(InvalideDonneesException e5) {
            // Erreur
            request.setAttribute("errorInvalideDonnees", e5.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(DataException e6) {
            // Erreur
            request.setAttribute("error", e6.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
